<?php
class UploadController extends BaseController
{
    public function __construct($uri)
    {
        parent::__construct($uri);
    }

    public function add()
    {
        $params = array('callback', 'redirect_uri');
        $this->checkParams($params);
        $this->output['tpl'] = 'upload';
        $this->output['data'] = $this->input;
        return $this->output;
    }

    public function addSubmit()
    {
        $params = array('callback', 'redirect_uri');
        $this->checkParams($params);
        $m = new FileModel();
        $r = $this->input;
        $r['filename'] = $m->add($_FILES['file']);
        $this->output['tpl'] = 'upload_submit';
        $this->output['data'] = $r;
        return $this->output;
    }

    public function route()
    {
        if(1 === preg_match('/^\/upload?.+$/', $this->uri)) {
            switch($this->http_method) {
                case 'get' :
                    return $this->add();
                    break;
                case 'post' :
                    return $this->addSubmit();
                    break;
            }
        }

        throw new ControllerException();
    }
}
?>
